package com.tanhua.server.controller;

import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.vo.NearUserVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.TanhuaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author げっしょく
 * @date 2021/10/25
 */
@RestController
@RequestMapping("/tanhua")
public class TanhauController {

    @Autowired
    private TanhuaService tanhuaService;

    /**
     * 查询今日佳人(缘分值最高的妹妹)
     * @return 返回佳人的具体信息和缘分值
     */
    @GetMapping("/todayBest")
    public ResponseEntity todayBest() {
        TodayBest todayBest = tanhuaService.todayBest();
        return ResponseEntity.ok(todayBest);
    }

    /**
     * 首页的推荐用户
     * @param recommendUserDto 页码，条数，推荐条件
     * @return 通过条件查询到的数据
     */
    @GetMapping("/recommendation")
    public ResponseEntity recommendation(RecommendUserDto recommendUserDto) {
        PageResult pageResult = tanhuaService.recommendation(recommendUserDto);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐人的详细信息
     * @param id
     * @return
     */
    @GetMapping("/{id}/personalInfo")
    public ResponseEntity personalInfo(@PathVariable Long id) {
        TodayBest todayBest = tanhuaService.getUserInfo(id);
        return ResponseEntity.ok(todayBest);
    }

    /**
     * 查寻陌生人问题
     * @param userId
     * @return
     */
    @GetMapping("/strangerQuestions")
    public ResponseEntity strangerQuestions(Long userId) {
        String txt = tanhuaService.getQuestion(userId);
        return ResponseEntity.ok(txt);
    }

    /**
     * 回复陌生人消息
     * @param map
     * @return
     */
    @PostMapping("/strangerQuestions")
    public ResponseEntity strangerQuestions(@RequestBody Map map) {
        //获取需要被回复的人的id
        Long userId = Long.valueOf(map.get("userId").toString());
        String reply = map.get("reply").toString();
        tanhuaService.sendMessage(userId, reply);
        return ResponseEntity.ok(null);

    }

    /**
     * 主页的探花功能
     * @return
     */
    @GetMapping("/cards")
    public ResponseEntity cards() {
        List<TodayBest> list = tanhuaService.cards();
        return ResponseEntity.ok(list);
    }


    /**
     * 探花:右滑
     * @param id
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable Long id) {
        tanhuaService.saveOrUpdate(id, true);
        return ResponseEntity.ok(null);
    }


    /**
     * 探花:左滑
     * @param id
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable Long id) {
        tanhuaService.saveOrUpdate(id, false);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询附近的人
     * @param gender
     * @param distance
     * @return
     */
    @GetMapping("/search")
    public ResponseEntity search(String gender, @RequestParam(defaultValue = "2000") String distance) {
        List<NearUserVo> list = tanhuaService.queryNearby(gender, distance);
        return ResponseEntity.ok(list);
    }
}
